package textTests;
import globalCode.BondMap;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;




public class TestOne {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		String url = "jdbc:mysql://ischooldev.fcps.net:3306/";
		String username = "root";
		String password = "PAss33word!!";
		Statement stmt = null;
		ResultSet rs = null;
		Connection con;
		
		try {
			con = DriverManager.getConnection(url, username, password);
			stmt = con.createStatement();
			stmt.executeQuery("USE inetwork");
			rs = stmt.executeQuery("SELECT * FROM group_member WHERE member = 3");
			rs.last();
			int[] groups = new int[rs.getRow()];
			rs.beforeFirst();
			for(int i = 0; i<groups.length; i++){
				rs.next();
				groups[i] = rs.getInt(2);
			}
			BondMap<Integer, Integer> bm = new BondMap<Integer, Integer>();
			for(int i = 0; i<groups.length; i++){
				System.out.println("SELECT * FROM group_member WHERE 'group' = "+i);
				rs = stmt.executeQuery("SELECT * FROM group_member WHERE 'group' = "+i);
				rs.beforeFirst();
				System.out.println("jkl;");
				while(rs.next()){
					System.out.println("asdf");
					rs.next();
					int usrID = rs.getInt(2);
					if(usrID != 4)
						bm.put(usrID);
				}
			}
			
			int maxUser = 0, maxVal = 0;
			
			for(Integer i: bm.keySet()){
				if(bm.get(i)>maxVal){
					maxVal = bm.get(i);
					maxUser = i;
				}
			}
			
			
			rs = stmt.executeQuery("SELECT * FROM usr WHERE id = "+maxUser);
			rs.first();
			System.out.println("There are "+maxVal+" groups that Jeffrey Jones and "+rs.getString(2)+" share.");
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			if(rs!=null){
				try{
					rs.close();
				} catch(SQLException sqlEx){}
				
				rs = null;
			}
			
			if(stmt!=null){
				try{
					stmt.close();
				} catch(SQLException sqlEx){}
				
				stmt = null;
			}
		}
		System.out.println("Hi");
		
		

	}

}
